package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.CrossActivityDetail;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eUnionActivityGroupType;
import com.yanqu.road.entity.enums.eUnionActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manger.TempCommonMgr;
import com.yanqu.road.server.manger.TempGoodsBagMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityMgr extends TempMgr {

    private static Map<Integer, ActivityInfo> activityMap;

    private static Map<Integer, List<ActivityConditionInfo>> activityConditionMap;

    private static Map<Integer, Map<Integer,CrossActivityDetail>> activityDetailMap = new ConcurrentHashMap<>();

    public static boolean activityInShowTime(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return now >= activityInfo.getBeginShowTime() && now <= activityInfo.getEndShowTime();
    }

    public static boolean activityInTime(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return now >= activityInfo.getBeginTime() && now <= activityInfo.getEndTime();
    }


    @Override
    public boolean reloadData() {
        loadActivity();
        return true;
    }

    public static int getRankListShowNum() {
        return 500;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void loadActivity(){
        Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getAllRunningActivityInfoMap();
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            eUnionActivityGroupType groupType = eUnionActivityGroupType.getType(activityInfo.getType());
            if (groupType == null) {
                if (activityInfo.getCrossId() == CrossBaseServer.getInstance().getServerId()) {
                    activityInfoMap.put(activityInfo.getActivityId(), activityInfo);
                }
            } else {
                if (!UnionActivityGroupHelper.needGroup(activityInfo)) {
                    if (activityInfo.getCrossId() == CrossBaseServer.getInstance().getServerId()) {
                        activityInfoMap.put(activityInfo.getActivityId(), activityInfo);
                    }
                }else {
                    Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
                    if (null != activityGroupMap) {
                        for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                            if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                                activityInfoMap.put(activityInfo.getActivityId(), activityInfo);
                            }
                        }
                    }
                }

            }
        }

        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(activityIdList);
        activityMap = activityInfoMap;
        activityConditionMap = activityConditionInfoMap;

        Map<Integer, Map<Integer, CrossActivityDetail>> tempActivityDetailMap = ActivityBussiness.getCrossActivityDetailMap(activityIdList);
        tempActivityDetailMap.putAll(activityDetailMap);
        activityDetailMap = tempActivityDetailMap;

        // 每次重新加载活动的时候 也加载下道具配置吧 很怂
        TempCommonMgr.reloadCommonData();
        TempGoodsBagMgr.reloadGoodsBagMap();
    }

    /**
     * 获取所有活动
     * @return
     */
    public static Map<Integer, ActivityInfo> getActivityMap() {
        return new ConcurrentHashMap<>(activityMap);
    }

    public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionMap() {
        return new ConcurrentHashMap<>(activityConditionMap);
    }

    public static Map<Integer, CrossActivityDetail> getActivityDetailMap(int activityId) {
        Map<Integer, CrossActivityDetail> mapData = activityDetailMap.get(activityId);
        if (null == mapData){
            synchronized (activityDetailMap){
                mapData = activityDetailMap.get(activityId);
                if (null == mapData){
                    mapData = new ConcurrentHashMap<>();
                    activityDetailMap.put(activityId,mapData);
                }
            }
        }
        return mapData;
    }

    public static CrossActivityDetail getCrossActivityDetail(int activityId, int groupId, boolean newIfNull) {
        Map<Integer, CrossActivityDetail> mapData = getActivityDetailMap(activityId);
        CrossActivityDetail crossActivityDetail = mapData.get(groupId);
        if (crossActivityDetail == null && newIfNull) {
            synchronized (activityDetailMap) {
                crossActivityDetail = mapData.get(groupId);
                if (crossActivityDetail == null) {
                    crossActivityDetail = new CrossActivityDetail();
                    crossActivityDetail.setActivityId(activityId);
                    crossActivityDetail.setGroupId(groupId);
                    crossActivityDetail.setInsertOption();
                    mapData.put(crossActivityDetail.getGroupId(), crossActivityDetail);
                }
            }
        }
        return crossActivityDetail;
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId){
        if (activityConditionMap == null) {
            return new ArrayList<>();
        }
        List<ActivityConditionInfo> activityConditionInfoList = activityConditionMap.get(activityId);
        if(null != activityConditionInfoList){
            return new ArrayList<>(activityConditionInfoList);
        }
        return new ArrayList<>();
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId, int conditionId){
        List<ActivityConditionInfo> activityConditionInfoList = getActivityConditionInfoList(activityId);
        for(ActivityConditionInfo activityConditionInfo : activityConditionInfoList){
            if(activityConditionInfo.getConditionId() == conditionId){
                return activityConditionInfo;
            }
        }
        return null;
    }

    public static Map<Integer, ActivityInfo> getOpenCrossRankActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if (ActivityHelper.activityInShowTime(activityInfo) &&
                    (activityInfo.getType() == eActivityType.CrossRankActivity.getValue() ||
                            activityInfo.getType() == eActivityType.MusicRoomRank.getValue()||
                            activityInfo.getType() == eActivityType.BowArtActivity.getValue())) {
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    public static Map<Integer, ActivityInfo> getOpenHuaShiGangRankActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue()){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    public static Map<Integer, ActivityInfo> getOpenDunHuangShiKuRankActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.CrossDunHuangShiKuActivity.getValue()){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    public static Map<Integer, ActivityInfo> getOpenCrossCommonActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.CrossCommonActivity.getValue()){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    public static Map<Integer, ActivityInfo> getOpenCrossActivityMapByType(int activityType){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == activityType){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    /**
     * 获取活动，根据活动类型集合
     * @param activityTypes
     * @return
     */
    public static Map<Integer, ActivityInfo> getOpenCrossActivityMapByTypes(Set<Integer> activityTypes){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityTypes.contains(activityInfo.getType())){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    /**
     * 获取新的开放的冲榜活动集合
     * @return
     */
    public static Map<Integer, ActivityInfo> getOpenNewRankActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && (
                    activityInfo.getType() == eActivityType.PageantActivity.getValue() ||
                    activityInfo.getType() == eActivityType.CrossHolidayActivity.getValue() ||
                    activityInfo.getType() == eActivityType.ThrowingPotActivity.getValue() ||
                    activityInfo.getType() == eActivityType.YearMasterActivity.getValue() ||
                    activityInfo.getType() == eActivityType.TreasureActivity.getValue() ||
                    activityInfo.getType() == eActivityType.PplActivity.getValue()) ||
                    activityInfo.getType() == eActivityType.DrawPicturesActivity.getValue() ||
                    activityInfo.getType() == eActivityType.DaFuWeng.getValue() ||
                    activityInfo.getType() == eActivityType.JianChuangHuaActivity.getValue() ||
                    activityInfo.getType() == eActivityType.TangHuluActivity.getValue() ||
                    activityInfo.getType() == eActivityType.ShuiHuActivity.getValue() ||
                    activityInfo.getType() == eActivityType.BowArtActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GhostActivity.getValue() ||
                    activityInfo.getType() == eActivityType.SuoYaoActivity.getValue() ||
                    activityInfo.getType() == eActivityType.BusyFarmingActivity.getValue() ||
                    activityInfo.getType() == eActivityType.HangHaiActivity.getValue()  ||
                    activityInfo.getType() == eActivityType.PawnshopActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GuGuoXunBaoActivity.getValue() ||
                    activityInfo.getType() == eActivityType.DrinkActivity.getValue() ||
                    activityInfo.getType() == eActivityType.DragonBoatRaceActivity.getValue() ||
                    activityInfo.getType() == eActivityType.XiaYueActivity.getValue() ||
                    activityInfo.getType() == eActivityType.CrossDunHuangShiKuActivity.getValue() ||
                    activityInfo.getType() == eActivityType.CelebrationActivity.getValue() ||
                    activityInfo.getType() == eActivityType.LanRuoSiActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GourdChildActivity.getValue()||
                    activityInfo.getType() == eActivityType.WinterLoveActivity.getValue() ||
                    activityInfo.getType() == eActivityType.YingXinActivity.getValue() ||
                    activityInfo.getType() == eActivityType.HellActivity.getValue() ||
                    activityInfo.getType() == eActivityType.XiaowuRecallActivity.getValue()  ||
                    activityInfo.getType() == eActivityType.DouLuoDaLuActivity.getValue() ||
                    activityInfo.getType() == eActivityType.DouLuoShrekActivity.getValue()||
                    activityInfo.getType() == eActivityType.ShuiHuYeZiCardActivity.getValue()||
                    activityInfo.getType() == eActivityType.SpringDinner.getValue() ||
                    activityInfo.getType() == eActivityType.SeacraftActivity.getValue()||
                    activityInfo.getType() == eActivityType.ToyHouse.getValue() ||
                    activityInfo.getType() == eActivityType.ChineseChess.getValue()||
                    activityInfo.getType() == eActivityType.ShuShanActivity.getValue() ||
                    activityInfo.getType() == eActivityType.ZhouNianTurntable.getValue() ||
                    activityInfo.getType() == eActivityType.ClimbTower.getValue() ||
                    activityInfo.getType() == eActivityType.OperaMask.getValue()  ||
                    activityInfo.getType() == eActivityType.RankUnionWar.getValue() ||
                    activityInfo.getType() == eActivityType.FellingTrees.getValue() ||
                    activityInfo.getType() == eActivityType.Pasture.getValue() ||
                    activityInfo.getType() == eActivityType.Tomb.getValue() ||
                    activityInfo.getType() == eActivityType.GreatTradeActivity.getValue() ||
                    activityInfo.getType() == eActivityType.Twins.getValue() ||
                    activityInfo.getType() == eActivityType.CricketSeasonActivity.getValue() ||
                    activityInfo.getType() == eActivityType.Cat.getValue() ||
                    activityInfo.getType() == eActivityType.WorldCup.getValue() ||
                    activityInfo.getType() == eActivityType.PeakStrongHold.getValue() ||
                    activityInfo.getType() == eActivityType.LionDance.getValue() ||
                    activityInfo.getType() == eActivityType.CarpTripActivity.getValue() ||
                    activityInfo.getType() == eActivityType.FireWork2023.getValue() ||
                    activityInfo.getType() == eActivityType.FanXiuSpringActivity.getValue() ||
                    activityInfo.getType() == eActivityType.XiuXian.getValue() ||
                    activityInfo.getType() == eActivityType.Feast.getValue() ||
                    activityInfo.getType() == eActivityType.FlowerCards.getValue()||
                    activityInfo.getType() == eActivityType.JieShi.getValue() ||
                    activityInfo.getType() == eActivityType.PeakSeacraftActivity.getValue() ||
                    activityInfo.getType() == eActivityType.PeerlessBeautyLotteryActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GoddessActivity.getValue() ||
                    activityInfo.getType() == eActivityType.CommonWishDraw.getValue() ||
                    activityInfo.getType() == eActivityType.DeepFishing.getValue() ||
                    activityInfo.getType() == eActivityType.LangYaMemorial.getValue() ||
                    activityInfo.getType() == eActivityType.FiveHero.getValue() ||
                    activityInfo.getType() == eActivityType.LangYaBang.getValue() ||
                    activityInfo.getType() == eActivityType.SilkRoad.getValue() ||
                    activityInfo.getType() == eActivityType.FlowerStudy.getValue() ||
                    activityInfo.getType() == eActivityType.WanYao.getValue() ||
                    activityInfo.getType() == eActivityType.QiXiActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GuaGiftsActivity.getValue() ||
                    activityInfo.getType() == eActivityType.GoldAutumn.getValue() ||
                    activityInfo.getType() == eActivityType.DigGold.getValue() ||
                    activityInfo.getType() == eActivityType.EveFeast.getValue() ||
                    activityInfo.getType() == eActivityType.DaoMu.getValue() ||
                    activityInfo.getType() == eActivityType.WitchDraw.getValue() ||
                    activityInfo.getType() == eActivityType.SteleSoul.getValue() ||
                    activityInfo.getType() == eActivityType.MusicRoomRank.getValue() ||
                    activityInfo.getType() == eActivityType.SnowGarden.getValue() ||
                    activityInfo.getType() == eActivityType.ActivityFund.getValue() ||
                    activityInfo.getType() == eActivityType.RuiShou.getValue() ||
                    activityInfo.getType() == eActivityType.ZhenHuan.getValue() ||
                    activityInfo.getType() == eActivityType.BiaoJu.getValue() ||
                    activityInfo.getType() == eActivityType.SpringDraw.getValue() ||
                    activityInfo.getType() == eActivityType.FoxCard.getValue() ||
                    activityInfo.getType() == eActivityType.HongXianActivity.getValue() ||
                    activityInfo.getType() == eActivityType.SummerGames.getValue() ||
                    activityInfo.getType() == eActivityType.Harps.getValue() ||
                    activityInfo.getType() == eActivityType.SonHai.getValue() ||
                    activityInfo.getType() == eActivityType.HsIsland.getValue() ||
                    activityInfo.getType() == eActivityType.Turtle.getValue() ||
                    activityInfo.getType() == eActivityType.HotpotJump.getValue() ||
                    activityInfo.getType() == eActivityType.Gashapon.getValue() ||
                    activityInfo.getType() == eActivityType.Apprentice.getValue() ||
                    activityInfo.getType() == eActivityType.XingYouShanShui.getValue() ||
                    (activityInfo.getType() == eActivityType.ChefDuel.getValue() && activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()) ||     //厨神争霸一阶段处理
                    activityInfo.getType() == eActivityType.YQHotel.getValue()
            ) {
                    dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    /**
     * 获取跨服商会争霸活动
     * @return
     */
    public static Map<Integer, ActivityInfo> getOpenCrossUnionRankActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.CrossUnionActivity.getValue()){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            } else if (ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.DaFuWeng.getValue()) {
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            } else if (ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.UnionRankActivity.getValue()) {
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }else if (ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.RankUnionWar.getValue()) {
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    //跨服宴会冲榜是否在进行，在进行才能开跨服私人宴会
    public static Map<Integer,ActivityInfo> getRunningCrossBanquetRankActivity() {
        Map<Integer,ActivityInfo> map = new HashMap<>();
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.CrossRankActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInfo.getType() == eActivityType.CrossRankActivity.getValue() && activityInfo.getChildType() == 5) {
                map.put(activityInfo.getActivityId(),activityInfo);
            }
        }
        return map;
    }


    public static ActivityInfo getActivityInfo(int activityId) {
        if (activityMap == null) {
            return null;
        }
        return activityMap.get(activityId);
    }

    /**
     *  获取活动配置，如果内存没有该活动配置，去加载数据库并且把配置存入内存
     * @param activityId    活动id
     * @return
     */
    public static ActivityInfo getActivityInfoMayLoadDB(int activityId) {
        ActivityInfo activityInfo = activityMap.get(activityId);
        if (activityInfo == null) {
            synchronized (ActivityMgr.class){
                activityInfo = activityMap.get(activityId);
                if(activityInfo == null){
                    ActivityInfo tempActivityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if(ActivityHelper.activityInShowTime(tempActivityInfo)){
                        List<Integer> activityIdList = new ArrayList<>();
                        activityIdList.add(activityId);
                        Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(activityIdList);
                        activityMap.put(activityId,tempActivityInfo);
                        activityConditionMap.putAll(activityConditionInfoMap);

                        Map<Integer,Map<Integer,CrossActivityDetail>> tempActivityDetailMap = ActivityBussiness.getCrossActivityDetailMap(activityIdList);
                        activityDetailMap.putAll(tempActivityDetailMap);
                        activityInfo = tempActivityInfo;
                    }
                }
            }
        }
        return activityInfo;
    }


    public static void putActivity(ActivityInfo activityInfo, List<ActivityConditionInfo> conditionInfoList){
        if(activityMap.get(activityInfo.getActivityId()) != null){
            return;
        }
        activityMap.put(activityInfo.getActivityId(), activityInfo);
        activityConditionMap.put(activityInfo.getActivityId(), conditionInfoList);

        // 每次重新加载活动的时候 也加载下道具配置吧 很怂
        TempCommonMgr.reloadCommonData();
        TempGoodsBagMgr.reloadGoodsBagMap();
    }

    @Override
    public boolean save() {
        for(Map<Integer, CrossActivityDetail> item : activityDetailMap.values()){
            for (CrossActivityDetail activityDetail : item.values()) {
                if(activityDetail.isInsertOption()){
                    ActivityBussiness.addCrossActivityDetail(activityDetail);
                }else if(activityDetail.isUpdateOption()){
                    ActivityBussiness.updateCrossActivityDetail(activityDetail);
                }
            }
        }
        return true;
    }

    public static Map<Integer, ActivityInfo> getOpenActivityInfoByTypeAndChildType(int activityType) {
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : ActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo) && activityInfo.getType() == activityType){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }
}
